package com.sunnyatwal.kettlebell;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.NumberPicker;
import android.widget.Toast;

import com.sunnyatwal.kettlebell.model.Exercise;
import com.sunnyatwal.kettlebell.model.ExerciseUnit;
import com.sunnyatwal.kettlebell.model.WorkoutBuilder;

public class ShockClockActivity extends Activity {

	private static final String TAG = "ShockClockActivity";
	
	private final static String INTENT_NAME_EXERCISE = "exercise";
	
	
	@Override 
	protected void onStart() {
		Log.i(TAG, "onStart");
		super.onStart();
	}
	
	
	@Override
	protected void onStop() {
    	Log.i(TAG, "onStop");
    	super.onStop();
    }
	
	
	@Override
	protected void onRestart() {
		Log.i(TAG, "onRestart");
		super.onRestart();
	}
	
	
	@Override
	protected void onResume() {
		Log.i(TAG, "onResume");
		super.onResume();		    			
	}
	
	
	@Override
	protected void onDestroy() {
		Log.i(TAG, "onDestroy");
		super.onDestroy();		
	}
	
	
	@Override
    public void onBackPressed() {
    	Log.i(TAG, "onBackPressed");
		super.onBackPressed();
    }
	
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    	Log.i(TAG, "onCreate");
    	
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.activity_shock_clock);
        
        getActionBar().setDisplayHomeAsUpEnabled(false);
        getActionBar().setDisplayShowTitleEnabled(false);
                
    	MainMenuHelper mmhelper = MainMenuHelper.getInstance();
        mmhelper.createMenuButtons(this);
        
        NumberPicker npRound = (NumberPicker) findViewById(R.id.shock_clock_round);
        npRound.setMinValue(1);
        npRound.setMaxValue(100);
        npRound.setWrapSelectorWheel(false);
        npRound.setValue(1);
        
        NumberPicker npWorkMins = (NumberPicker) findViewById(R.id.shock_clock_work_mins);
        npWorkMins.setMinValue(0);
        npWorkMins.setMaxValue(59);
        npWorkMins.setWrapSelectorWheel(true);
        npWorkMins.setValue(0);
        
        NumberPicker npWorkSecs = (NumberPicker) findViewById(R.id.shock_clock_work_secs);
        npWorkSecs.setMinValue(0);
        npWorkSecs.setMaxValue(59);
        npWorkSecs.setWrapSelectorWheel(true);
        npWorkSecs.setValue(30);
        
        NumberPicker npRestMins = (NumberPicker) findViewById(R.id.shock_clock_rest_mins);
        npRestMins.setMinValue(0);
        npRestMins.setMaxValue(59);
        npRestMins.setWrapSelectorWheel(true);
        npWorkSecs.setValue(0);
        
        NumberPicker npRestSecs = (NumberPicker) findViewById(R.id.shock_clock_rest_secs);
        npRestSecs.setMinValue(0);
        npRestSecs.setMaxValue(59);
        npRestSecs.setWrapSelectorWheel(true);     
        npRestSecs.setValue(30);
    }

    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    	Log.i(TAG, "onCreateOptionsMenu");
    	
        getMenuInflater().inflate(R.menu.activity_shock_clock, menu);
        return true;
    }

    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    	Log.i(TAG, "onOptionsItemSelected");
    	
        switch (item.getItemId()) {
        
            case android.R.id.home:
                NavUtils.navigateUpFromSameTask(this);
                break;
                
            case R.id.menu_shock_clock_play:
            	startClockActivity();
                break;
                
        }
        
        return true;
    }
    
    
	private void startClockActivity() {
		Log.i(TAG, "onOptionsItemSelected");
		
		NumberPicker npRound = (NumberPicker) findViewById(R.id.shock_clock_round);
        NumberPicker npWorkMins = (NumberPicker) findViewById(R.id.shock_clock_work_mins);
        NumberPicker npWorkSecs = (NumberPicker) findViewById(R.id.shock_clock_work_secs);
        NumberPicker npRestMins = (NumberPicker) findViewById(R.id.shock_clock_rest_mins);
        NumberPicker npRestSecs = (NumberPicker) findViewById(R.id.shock_clock_rest_secs);
        
        int iRounds = npRound.getValue();
        
        String name = getString(R.string.string_shock_clock);
        
        //convert to milliseconds        
        int workTime = ExerciseUnit.convert(npWorkMins.getValue(), npWorkSecs.getValue(), true);
        int restTime = ExerciseUnit.convert(npRestMins.getValue(), npRestSecs.getValue(), true);
		
        if(workTime > 0 && restTime > 0) {
        
	        Exercise exercise = WorkoutBuilder.generateWorkout(name, iRounds, workTime, restTime);       
        	
	        Intent intent = new Intent(ShockClockActivity.this, ClockActivity.class);                		
	    	intent.putExtra(INTENT_NAME_EXERCISE, exercise);
			startActivity(intent);
        }
        else {
        	Toast toast = Toast.makeText(getApplicationContext(), 
					getString(R.string.string_zero_time), Toast.LENGTH_SHORT);
			toast.setGravity(Gravity.TOP | Gravity.CENTER, 0, 30);
			toast.show();
        }
	}

}
